package com.cskaoyan.controller;

import com.cskaoyan.annotation.LogAnnotation;
import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin/auth")
public class AuthenticateController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminService adminService;
    @Autowired
    RoleMapper roleMapper;


    @RequestMapping("/login")
    public BaseRespVo login(@RequestBody User user){

//        Subject subject = SecurityUtils.getSubject();
//        try {
//            subject.login(new UsernamePasswordToken(user.getUsername(),user.getPassword()));
//        } catch (AuthenticationException e) {
//            unAuthc();
////            e.printStackTrace();
//        }
//
//        System.out.println(subject.isAuthenticated());
//        return BaseRespVo.ok("");

        MallToken adminToken = new MallToken(user.getUsername(),user.getPassword(), "admin");
        Subject subject = SecurityUtils.getSubject();
        subject.login(adminToken);
        System.out.println("admin/auth/login shiro验证:"+subject.isAuthenticated()+" sid："+subject.getSession().getId());

        return BaseRespVo.ok(subject.getSession().getId());
    }

    @RequestMapping("/unAuthc")//失败重定向
    public String unAuthc(){
        System.out.println("失败");
        return "forward:/admin/auth/login";
    }

    @LogAnnotation(module = "退出")
    @RequestMapping("/logout")
    public BaseRespVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    @LogAnnotation(module = "登录")
    @RequestMapping("/info")
    public BaseRespVo info(String token){
        Subject subject = SecurityUtils.getSubject();

        Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
        MallToken mallToken=(MallToken) primaryPrincipal;

        String username = mallToken.getUsername();
        char[] password1 = mallToken.getPassword();
        String password = String.valueOf(password1);

        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);

        DataBean dataBean = new DataBean();

//<<<<<<< HEAD
//        dataBean.setAvatar("http://localhost:8083/picture.jpg");
//        dataBean.setName("ligenli");
//        ArrayList<String> perms = new ArrayList<>();
//        perms.add("*");
//        dataBean.setPerms(perms);
//
//        ArrayList<String> roles = new ArrayList<>();
//        roles.add("超级管理员");
//        dataBean.setRoles(roles);
//=======

//        dataBean.setAvatar(admin.getAvatar());
        dataBean.setAvatar(admin.getAvatar());
        dataBean.setName(admin.getUsername());

        List<String> permission = adminService.selectPermissionByUsername(username);
        dataBean.setPerms(permission);

        Integer[] roleIds = admin.getRoleIds();
        ArrayList<String> strings = new ArrayList<>();
        for (Integer i:roleIds){
            Role role = roleMapper.selectByPrimaryKey(i);
            strings.add(role.getName());
        }
        boolean[] booleans = subject.hasRoles(strings);
        System.out.println("授权+"+booleans);
        dataBean.setRoles(strings);


        return BaseRespVo.ok(dataBean);
    }
}
